package com.example.ssm.controller;

import com.example.ssm.user.User;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
    @Controller
    @RequestMapping("user")
    public class UserController extends BaseController{
        private BasicDataSource basicDataSource;
        @Autowired
        public void setBasicDataSource(BasicDataSource basicDataSource) {
            this.basicDataSource = basicDataSource;
        }
        @RequestMapping(value = "SignIn",method = RequestMethod.POST)
        public String signIn(){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String selectSql = "select * from db_user.user where username = ? and password = ?";
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                connection = basicDataSource.getConnection();
                preparedStatement = connection.prepareStatement(selectSql);
                preparedStatement.setString(1,username);
                preparedStatement.setString(2,password);
                resultSet = preparedStatement.executeQuery();
                if (resultSet.next()){
                    String user_username = resultSet.getString("username");
                    String user_password = resultSet.getString("password");
                    User user = new User(user_username,user_password);
                    session.setAttribute("user",user);
                    return "redirect:/home.jsp";
                }else {
                    session.setAttribute("error","check your username and password!!");
                    return "redirect:/index.jsp";
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }finally {
                close(resultSet,preparedStatement,connection);
            }
        }

        @RequestMapping(value = "SignUp",method = RequestMethod.POST)
        public String signUp(){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String insertSql = "insert into db_user.user values (null,?,?)";
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            try {
                connection = basicDataSource.getConnection();
                preparedStatement = connection.prepareStatement(insertSql);
                preparedStatement.setString(1,username);
                preparedStatement.setString(2,password);
                preparedStatement.executeUpdate();
                session.setAttribute("message","insert successfully!!");
                return "redirect:/index.jsp";
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }finally {
                close(null,preparedStatement,connection);
            }
        }
}
